package com.huidz.calendar.activity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.transition.Slide;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.huidz.calendar.R;
import com.huidz.calendar.util.Apiutils;
import com.huidz.calendar.util.Url;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class User_ZhuceActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView byorzy;
    private int passwordtype = 0;//默认情况下密码不显示明文
    private EditText edit_password, zcqr_edit, edit_phone, edit_phone_num, code_password;

    private String phonestr, passwordstr;
    private String mobile, codes, pwd, os = "android";

    private int submit = 0, soup = 0;

    private RelativeLayout submit_user, get_soupt;
    private ImageView comeback;

    private TextView get_code, pwd_login_text, luck_title_text;

    private LinearLayout phone_login, pwd_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.BOTTOM);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.activity_zhuce);
        inti();
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        get_code = findViewById(R.id.get_code);
        get_code.setOnClickListener(this);
        comeback = findViewById(R.id.title_comeback);
        code_password = findViewById(R.id.code_password);
        comeback.setOnClickListener(this);
        edit_phone_num = findViewById(R.id.edit_phone_num);
        pwd_login_text = findViewById(R.id.pwd_login_text);
        pwd_login_text.setOnClickListener(this);
        byorzy = findViewById(R.id.byorzy);
        byorzy.setOnClickListener(this);
        edit_password = findViewById(R.id.edit_password);
        edit_phone = findViewById(R.id.edit_phone);
        submit_user = findViewById(R.id.submit_user);
        get_soupt = findViewById(R.id.get_soupt);
        get_soupt.setOnClickListener(this);
        submit_user.setOnClickListener(this);
        phone_login = findViewById(R.id.phone_login);
        pwd_login = findViewById(R.id.pwd_login);
        luck_title_text = findViewById(R.id.luck_title_text);
        luck_title_text.setOnClickListener(this);
        zcqr_edit = findViewById(R.id.zcqr_edit);
        edit_phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!charSequence.toString().equals("")) {
                    submit_user.setBackgroundResource(R.drawable.rela_selected_bg_red);
                    submit = 1;
                } else {
                    submit_user.setBackgroundResource(R.drawable.rela_selected_bg_huise);
                    submit = 0;
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        edit_phone_num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!charSequence.toString().equals("")) {
                    get_soupt.setBackgroundResource(R.drawable.rela_selected_bg_red);
                    soup = 1;
                } else {
                    get_soupt.setBackgroundResource(R.drawable.rela_selected_bg_huise);
                    soup = 0;
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        edit_password.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE) {
                }
                return false;
            }
        });
        edit_phone_num.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE) {
                    if (!edit_phone_num.getText().equals("")) {
                        Intent intent = new Intent(User_ZhuceActivity.this, GetCodeActivity.class);
                        intent.putExtra("phonenum", edit_phone_num.getText().toString());
                        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(User_ZhuceActivity.this).toBundle());
                    }
                }
                return false;
            }
        });
        getcodtime();
    }

    private Handler handler = new Handler();
    private Runnable runnable;
    private int tiemid = 60, setpost = 0;

    public void getcodtime() {
        runnable = new Runnable() {
            public void run() {
                if (tiemid == 0) {
                    StopTime();
                } else {
                    tiemid--;
                    get_code.setText(tiemid + "秒后获取");
                    handler.postDelayed(this, 1000);
                }
            }
        };
    }

    public void StopTime() {
        setpost = 0;
        tiemid = 60;
        handler.removeCallbacks(runnable);
        get_code.setText("获取验证码");
    }

    public void okHttp_postFromParameters(final int code) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    if (code == 0) {
                        RequestBody formBody = new FormBody.Builder().add("mobile", mobile)
                                .build();
                        Request request = new Request.Builder().url(Url.GETCODE).post(formBody).build();
                        okhttp3.Response response = okHttpClient.newCall(request).execute();
                    } else {
                        RequestBody formBody = new FormBody.Builder().add("mobile", mobile).add("password", pwd).add("os", os).add("code", codes)
                                .build();
                        Request request = new Request.Builder().url(Url.ZHUCE).post(formBody).build();
                        okhttp3.Response response = okHttpClient.newCall(request).execute();
                        String msg = response.body().string();
                        Message message = new Message();
                        message.obj = msg;
                        gethot.sendMessage(message);
                    }
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public Handler gethot = new Handler() {
        public void handleMessage(Message msg) {
            String msgs = null;
            try {
                JSONObject success_Object = null;
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    msgs = success_Object.getString("msg");
                    if (code.equals("1")) {
                        onBackPressed();
                    } else
                        Apiutils.SetToast(User_ZhuceActivity.this, msgs + "");
                }
            } catch (
                    Exception e) {
                e.printStackTrace();
            }
        }
    };


    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.get_code:
                if (edit_phone.length() != 11) {
                    Apiutils.SetToast(User_ZhuceActivity.this, "手机号码有误");
                } else {
                    if (setpost == 0) {
                        mobile = edit_phone.getText().toString();
                        okHttp_postFromParameters(0);
                        handler.postDelayed(runnable, 1000);
                        setpost = 1;
                    }
                }
                break;
            case R.id.title_comeback:
                onBackPressed();
                break;
            case R.id.byorzy:
                SetEdPassWord();
                break;
            case R.id.submit_user:
                if (submit == 1) {
                    if (edit_phone.getText().equals("")) {
                        Apiutils.SetToast(User_ZhuceActivity.this, "请输入手机号");
                    } else if (code_password.getText().equals("")) {
                        Apiutils.SetToast(User_ZhuceActivity.this, "请输入验证码");
                    } else if (edit_password.getText().equals("")) {
                        Apiutils.SetToast(User_ZhuceActivity.this, "请输入密码");
                    } else if (zcqr_edit.getText().equals("")) {
                        Apiutils.SetToast(User_ZhuceActivity.this, "请确认密码");
                    } else {
                        mobile = edit_phone.getText().toString();
                        codes = code_password.getText().toString();
                        pwd = edit_password.getText().toString();
                        okHttp_postFromParameters(1);
                    }
                }


                break;
            case R.id.remenber_login:

                break;
            case R.id.pwd_login_text:
                phone_login.setVisibility(View.GONE);
                pwd_login.setVisibility(View.VISIBLE);
                luck_title_text.setVisibility(View.VISIBLE);
                break;
            case R.id.luck_title_text:

                break;
            case R.id.get_soupt:
                if (soup == 1) {

                }
                break;

        }
    }

    public void SetEdPassWord() {
        switch (passwordtype) {
            case 0:
                passwordtype++;
                edit_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                break;

            case 1:
                passwordtype--;
                edit_password.setInputType((InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD));
                break;
        }
    }

}
